package com.zcf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.ProductMapper;
import com.zcf.pojo.Product;
import com.zcf.service.ProductService;
import com.zcf.utils.UploadImgUtils;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/product")
@ResponseBody
@CrossOrigin
public class ProductController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductMapper productMapper;

	// 上传产品图片
	@PostMapping("/productImage")
	public Body productImage(MultipartFile[] image) {
		if (image != null && !"".equals(image)) {
			try {
				String file = UploadImgUtils.uploadFiles(image);
				return Body.newInstance(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Body.newInstance(501, "图片不能为空");
	}

	// 上传商品
	@PostMapping("/setProduct")
	public Body setProduct(String diningId, String productName, String image, Double price, String remark,
			String productNums, String classId,String isroom,String showcreate,String showend) {
		return productService.setProduct(diningId, productName, image, price, remark, productNums, classId, isroom,showcreate,showend);
	}

	// 查看店铺商品列表
	@PostMapping("/checkProductList")
	public Body checkProductList(String diningId, String type, String classId) {
		return productService.checkProductList(diningId, type, classId);
	}
	// 查看店铺商品列表All
	@PostMapping("/checkProductListAll")
	public Body checkProductList(String diningId) {
		return productService.checkProductListAll(diningId);
	}
	// 删除商品
	@PostMapping("/delete")
	public Body delete(String id) {
		Product product = new Product();
		product.setId(id);
		boolean b = product.deleteById();
		if (b) {
			return Body.newInstance(200, "删除成功");
		}
		return Body.newInstance(501, "删除有误");
	}

	/**
	 * 热门搜索
	 */
	@PostMapping("queryCount")
	public Body queryCount() {
		List<Product> lsits = productMapper.queryCount();
		if (lsits.size() > 0) {
			return Body.newInstance(lsits);
		}
		return Body.newInstance("查询失败");
	}

	/**
	 * 修改商品
	 */
	@PostMapping("updateProduct")
	public Body updateProduct(Product product) {
		Product product2 = new Product();
		product2.setId(product.getId());
		product2 = product2.selectById();
		if (product2 == null) {
			return Body.newInstance("商品不存在");
		}
		Boolean flag = product.updateById();
		if (flag) {
			return Body.newInstance(product);
		}
		return Body.BODY_500;
	}

	/**
	 * 商品详情
	 */
	@PostMapping("ProductOne")
	public Body ProductOne(String proId) {
		Product product = new Product();
		product.setId(proId);
		product = product.selectById();
		if (product != null) {
			return Body.newInstance(product);
		}
		return Body.BODY_451;
	}
}
